package com.hooray.elasticsearch;


import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.commonTermsQuery;
import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
import static org.elasticsearch.index.query.QueryBuilders.idsQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.elasticsearch.index.query.QueryBuilders.prefixQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
import static org.elasticsearch.index.query.QueryBuilders.simpleQueryStringQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;
import static org.elasticsearch.index.query.QueryBuilders.typeQuery;
import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery;

import java.io.IOException;

import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

/**
 * 
 * @Desc Elasticsearch常用操作的工具类
 *
 * @Author daijunjie
 * @DateTime 2016年4月14日 下午4:45:24
 *
 */
public class ElasticsearchUtil {
	
	public static final String CHARSET = "utf-8";
	
	/*********************************** XContentBuilder begin ***********************************/

	public static XContentBuilder buildXContentBuilder() throws IOException {
		return jsonBuilder();
	}

	
	public static XContentBuilder buildSimpleField(XContentBuilder builder, String fieldName, Object data) throws IOException {
		return builder.field(fieldName, data);
	}

	public static XContentBuilder buildComplexField(XContentBuilder builder, String fieldName, XContentType contentType, byte[] data) throws IOException {
		XContentParser parser = XContentFactory.xContent(contentType).createParser(data);
		parser.nextToken();
		// Add the field name, but not the value.
		builder.field(fieldName);
		try {
			// This will add the whole parsed content as the value of the field.
			builder.copyCurrentStructure(parser);
		} catch (Exception ex) {
			// If we get an exception here the most likely cause is nested JSON
			// that
			// can't be figured out in the body. At this point just push it
			// through
			// as is, we have already added the field so don't do it again
			builder.endObject();
			builder.field(fieldName, new String(data, CHARSET));
		} finally {
			if (parser != null) {
				parser.close();
			}
		}
		return builder;
	}

	
	/*********************************** XContentBuilder begin ***********************************/

	
	/*********************************** match query begin ***********************************/
	public static QueryBuilder buildMatchAllQuery() {
		return matchAllQuery();
	}

	public static QueryBuilder buildMatchQuery(String field, Object text) {
		return QueryBuilders.matchQuery(field, text);
	}

	public static QueryBuilder buildMultiMatchQuery(Object text, String... fieldNames) {
		return multiMatchQuery(text, fieldNames);
	}

	public static QueryBuilder buildCommonTermsQuery(String field, String text) {
		return commonTermsQuery(field, text);
	}

	public static QueryBuilder buildQueryStringQuery(String queryString) {
		return QueryBuilders.queryStringQuery(queryString);
	}

	public static QueryBuilder buildSimpleQueryStringQuery(String queryString) {
		return simpleQueryStringQuery(queryString);
	}

	/*********************************** match query end ***********************************/

	/*********************************** term query begin ***********************************/
	public static QueryBuilder buildTermQuery(String field, String... values) {
		return termQuery(field, values);
	}

	public static QueryBuilder buildTermsQuery(String field, String... values) {
		return termsQuery(field, values);
	}

	public static QueryBuilder buildRangeQuery(String field, long from, long to) {
		return rangeQuery(field).from(from).to(to);
	}

	public static QueryBuilder buildExistsQuery(String field) {
		return existsQuery(field);
	}

	public static QueryBuilder buildPrefixQuerys(String field, String prefix) {
		return prefixQuery(field, prefix);
	}

	public static QueryBuilder buildWildcardQuery(String field, String query) {
		return wildcardQuery(field, query);
	}

	public static QueryBuilder buildRegexpQuery(String field, String query) {
		return regexpQuery(field, query);
	}

	public static QueryBuilder buildFuzzyQuery(String field, String value) {
		return fuzzyQuery(field, value);
	}

	public static QueryBuilder buildTypeQuery(String type) {
		return typeQuery(type);
	}

	public static QueryBuilder buildIdsQuery(@Nullable String type, String... ids) {
		return idsQuery(type).addIds(ids);
	}

	/*********************************** term query end ***********************************/

}
